<template>
	<el-dialog
	  title="收样"
	  :visible.sync="visible"
	  :before-close="close"
	  :close-on-click-modal="false"
	  width="70%">
    注：此操作会记录在《样品接收与分发记录》，并确认以下说明
	  <el-row>
	    <el-col :span="24">
        <el-card>
          <el-form>
            <el-form-item
              v-for="(item, index) in dataList"
              :key="index"
              :prop="'dataList.' + index + 'getQuantity'">
                <span>
                  确定接收 {{item.getUserName}} 送出的样品
                  <span class="font-highlight">
                    <!-- 分类，品名 -->
                    <span>【{{item.checkTypeName}}】{{item.productName}}</span>
                    <span>(</span>
                    <!-- 批号 -->
                    <span>批号：{{item.batchNum}}</span>
                    <!-- 规格 -->
                    <span v-if="item.productSpec != null && item.productSpec != ''">,规格：<span v-html="item.productSpec" /></span>
                    <!-- 包装规格 -->
                    <span v-if="item.productPackSpec != null && item.productPackSpec != ''">,包装规格：{{item.productPackSpec}}</span>
                    <span>)</span>
                    <!-- 总取样量 -->
                    <span>{{item.getQuantity + item.getUnitName}}</span>
                    <!-- 特殊信息 -->
                    <span v-if="item.info != null && item.info != ''">{{'=> ' + item.info}}</span>
                  </span>
                </span>

              <el-date-picker
                v-model="item.receiveDate"
                align="right"
                type="date"
                placeholder="收样日期"
                :clearable="false"
                style="width: 100%; margin-top: 10px;"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
	  </el-row>

	  <span slot="footer" class="dialog-footer">
	    <el-button @click="close">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" :loading="submiting" @click="submit()">确 定</el-button>
  	</span>
	</el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { receive,getYsQuantityInfo } from '@/api/check'
	import { CODE_OK } from '@/common/js/config'
  import { isBlank,parseTime,dateCompare } from '@/common/js/utils'
	export default {
		data() {
			return {
				visible: false,
        submiting: false,
        dataList: []
			}
		},
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
        let date = new Date()
				this.dataList = this.childDataForm.map(item => {
          this.$set(item, 'receiveQuantity', item.getQuantity)
          this.$set(item, 'surplusQuantity', item.getQuantity)
          this.$set(item, 'receiveUnitId', item.getUnitId)
          this.$set(item, 'receiveUnitName', item.getUnitName)
          this.$set(item, 'receiveUserId', this.userInfo.userId)
          this.$set(item, 'receiveUserName', this.userInfo.name)
          this.$set(item, 'receiveDate', parseTime(date))
          this.$set(item, 'info', '')
          return item
        })
        this.visible = true
        this.getInfo()
			}
		},
    computed: {
      ...mapGetters([
         'userInfo'
      ])
    },
		methods: {
      getInfo() {
        for (let i=0,j=this.dataList.length; i<j; i++) {
          if (this.ys(this.dataList[i].checkTypeId)) {
            let params = {
              index: i,
              checkId: this.dataList[i].checkId
            }
            getYsQuantityInfo(params).then(res => {
              if (res.code === CODE_OK) {
                this.setYsInfo(res)
              }else{
                this.$message.error(res.msg)
                this.close()
              }
            })
          }
        }
      },
      setYsInfo(res) {
        this.dataList[res.index].info = res.info
        this.$set(this.dataList[res.index], 'crys', res.wqs.map(v => {
          this.$set(v,'reCount',v.count)
          return v
        }))
      },
			close(val) {
				this.$emit('close')
				if (val == 'submit') {
				  this.$emit('refreshData')
				}
				this.visible = false
			},
			submit() {
        if (!this.formVerify()) {
          return
        }
        this.submiting = true
        receive(this.dataList).then(res => {
          if (res.code === CODE_OK) {
            this.$message({
              message: '提交成功',
              type: 'success',
              duration: 1500
            })
            this.close("submit")
          }else{
            this.submiting = false
            this.$message.error(res.msg)
          }
        })
			},
      formVerify() {
        for (let item of this.dataList) {
          if (isBlank(item.receiveDate)) {
            this.$message.error("所有收样日期均不能为空")
            return false
          }
          if (!dateCompare(item.getDate, item.receiveDate)) {
            this.$message.error("所有收样日期必须晚于取样日期")
            return false
          }
        }
        return true
      }
    }
	}
</script>

<style>
</style>
